<%--
  Created by IntelliJ IDEA.
  User: bin.liu
  Date: 2019/6/25
  Time: 14:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<jsp:include page="/pages/sysIndex/default.jsp"/>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<style>
    .differentFlag {
        color: #FF0000;
    }

    .differentFlag1 {
        color: #0d8ddb;
    }
</style>
<div class="main-content">
    <div class="box box-main">
        <div class="box-header">
            <div class="box-title">
                <i class="fa icon-trophy"></i> 任务单管理
            </div>
            <div class="box-tools pull-right">
                <a href="#" class="btn btn-default" id="btnSearch" title="查询"><i class="fa fa-filter"></i> 查询</a>
                <%--<sec:authorize access="hasRole('ROLE_ENTERPRISE_TASK_ORDER_EDIT')">--%>
                    <a href="javascript:;" onclick="vLayuiUtil.openLayerWindow('wTaskOrder','<c:url
                            value="/web/admin/taskOrder/editPage.action"/>','新增',['80%', '95%'])"
                       class="btn btn-default btnTool" title="新增任务单"><i class="fa fa-plus"></i> 新增</a>
                    <a href="javascript:;" onclick="assign(0)"
                       class="btn btn-default btnTool" title="指定接单人"><i class="fa fa-plus"></i> 指定接单人</a>
                    <a href="javascript:;" onclick="assign(1)"
                       class="btn btn-default btnTool" title="指定岗位"><i class="fa fa-plus"></i> 指定岗位</a>
                    <a href="javascript:;" onclick="downloadData()" class="btn btn-default btnTool" title="导出"><i
                            class="fa fa-download"></i> 导出</a>
                <%--</sec:authorize>--%>
            </div>
        </div>

        <div class="box-body">
            <form id="searchForm" action="<c:url value="/web/admin/taskOrder/listJson.action"/>" method="post"
                  class="form-inline hide" data-page-no="1" data-page-size="20" data-order-by="">
                <div class="form-group">
                    <label class="control-label">流&nbsp;&nbsp;水&nbsp;&nbsp;号：</label>
                    <div class="control-inline">
                        <input type="text" id="serial" name="serial" value="" maxlength="64"
                               style="width: 200px"
                               class="form-control">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label">工单名称：</label>
                    <div class="control-inline">
                        <input type="text" id="orderName" name="orderName" value="" maxlength="64"
                               style="width: 200px"
                               class="form-control">
                    </div>
                </div>

                <div class="form-group">
                    <label class="control-label">工单状态：</label>
                    <div class="control-inline" style="width:200px">
                        <select id="orderStatus" name="orderStatus" class="form-control">
                            <option value="" selected>&nbsp;全部&nbsp;&nbsp;&nbsp;</option>
                            <option value="WAIT_TAKING">&nbsp;待接单&nbsp;</option>
                            <option value="WAIT_PRESENCE">&nbsp;待到场&nbsp;</option>
                            <option value="REJECTED">&nbsp;退回重做&nbsp;</option>
                            <option value="UNFINISHED">&nbsp;待完成&nbsp;</option>
                            <option value="IN_AUDITING">&nbsp;审批中&nbsp;</option>
                            <option value="COMPLETED">&nbsp;已完成&nbsp;</option>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label">发&nbsp;&nbsp;布&nbsp;&nbsp;人：</label>
                    <div class="control-inline">
                        <input type="text" id="createUser" name="createUser" value="" maxlength="64"
                               style="width: 200px"
                               class="form-control">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label">工单类型：</label>
                    <div class="control-inline">
                        <input type="text" id="orderTypeName" name="orderTypeName" value="" maxlength="64"
                               style="width: 200px"
                               class="form-control">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label">发布时间：</label>
                    <div class="control-inline">
                        <input type="text" id="createDate1" name="createDate1" value="" maxlength="64"
                               autocomplete="off"
                               style="width: 200px"
                               class="form-control">-
                        <input type="text" id="createDate2" name="createDate2" value="" maxlength="64"
                               autocomplete="off"
                               style="width: 200px"
                               class="form-control">
                    </div>
                </div>
                <div class="form-group">
                    <button type="submit" class="btn btn-primary btn-sm">查询</button>
                    <button type="reset" class="btn btn-default btn-sm">重置</button>
                </div>
            </form>

            <div style="overflow-x:auto">
                <table id="dataGrid"></table>
            </div>
            <div id="dataGridPage"></div>
        </div>
    </div>
</div>
<script>
    $('#dataGrid').dataGrid({
        shrinkToFit: false,
        autoScroll: true,
        showCheckbox: true,
        searchForm: $("#searchForm"),
        columnModel: [
            <sec:authorize access="hasRole('ROLE_ENTERPRISE_TASK_ORDER_EDIT')">
            {
                header: '操作',
                name: 'actions',
                width: 230,
                frozen: false,
                sortable: false,
                title: false,
                formatter: function (val, obj, row, act) {
                    var a = '<a class="btn btn-default btn-xs btnList" onclick="detail(' + row.id + ')" title="详情"><i class="fa fa-pencil">详情</i></a>&nbsp;';
                    if (row.orderStatusCode == 'WAIT_TAKING') {
                        a += '<a  class="btn btn-default btn-xs btnList" onclick="editTaskOrder(' + row.id + ')" title="编辑"><i class="fa fa-pencil"></i>编辑</a>&nbsp;';
                    }
                    a += '<a  class="btn btn-default btn-xs btnList" onclick="log(' + row.id + ')" title="日志"><i class="fa fa-file-text-o"></i>日志</a>&nbsp;';
                    <sec:authorize access="hasRole('ROLE_ENTERPRISE_TASK_ORDER_DELETE')">
                    a += '<a href="javascript:;" class="btn btn-default btn-xs btnList" onclick=" submitRemove(\'' + row.id + '\', \'0\')"><i class="fa fa-trash-o"></i>删除</a>&nbsp;';
                    </sec:authorize>
                    return a;
                }
            },
            </sec:authorize>

            {header: '主键id', name: 'id', index: 'id', width: 100, align: "center", edittype: "checkbox"},
            {header: '流水号', name: 'serial', index: 'serial', width: 250, align: "center"},
            {header: '工单名称', name: 'orderName', index: 'orderName', width: 150, align: "center", sortable: false},
            {header: '工单状态', name: 'orderStatusName', index: 'orderStatusName', width: 150, align: "center", sortable: false},
            {header: '工单类型',name: 'orderTypeName',index: 'orderTypeName',width: 150,align: "center",sortable: false},
            {header: '发布时间',name: 'createDate',index: 'createDate',width: 150,align: "center",sortable: true},
            {header: '要求完成时间',name: 'requireFinishTime',index: 'requireFinishTime',width: 150,align: "center",sortable: true},
            {header: '完成时间',name: 'taskFinishTime',index: 'taskFinishTime',width: 150,align: "center",sortable: true},
            {header: '发布人',name: 'createUser',index: 'createUser',width: 150,align: "center",sortable: false},
            {header: '审核人',name: 'auditUser',index: 'auditUser',width: 150,align: "center",sortable: false},
            {header: '地点', name: 'taskAddress', index: 'taskAddress', width: 200, align: "center"},
            {header: '任务来源', name: 'taskSourceName', index: 'taskSourceName', width: 100, align: "center"},
            {header: '任务内容',name: 'taskContent',index: 'taskContent',width: 200,align: "center",sortable: false},
            {header: '语音', name: 'word', index: 'word', width: 320, align: 'center',
                formatter: function (val, row) {
                    if ($.trim(val) === "") {
                        return ' — ';
                    } else {
                        return '<audio src="' + val + '" controls="controls" width="10%"></audio>';
                    }
                }
            },
            {header: '任务照片', name: 'taskPhoto1', index: 'taskPhoto1', width: 150, align: 'center',
                formatter: function (val, row) {
                    if ($.trim(val) === "") {
                        return ' — ';
                    } else {
                        return '<a target="_blank" href="' + val + '"><img style="height: 30px;" src="' + val + '"/></a>';
                    }
                }
            },
            {header: '是否指派', name: 'assign', index: 'assign', width: 150, align: "center", sortable: false},
            {header: '总工时', name: 'totalWorkingHours', index: 'totalWorkingHours', width: 100, align: "center"},
            {header: '接单人员工时', name: 'workingHours', index: 'workingHours', width: 100, align: "center"},
            {header: '所属企业',name: 'enterpriseName',index: 'enterpriseName',width: 150,align: "center",sortable: false},
        ],
        // 加载成功后执行事件
        ajaxSuccess: function (data) {
            //获取这一页所有的id
            var ids = $("#dataGrid").jqGrid("getDataIDs");
            //如果id存在
            if (ids) {
                //循环id
                for (var i = 0; i < ids.length; i++) {
                    //指定id行的数据
                    var rowData = $("#dataGrid").jqGrid("getRowData", ids[i]);
                    //不一致记录标红
                    if (rowData.orderStatus == "待接单") {
                        //插入样式，注意：当一个页面中包含多个jqgrid的时候，addClass的时候不能只根据id来添加，需要在id之前加上tableId '#report_grid_table '
                        $('#dataGrid ' + '#' + ids[i]).find("td").addClass("differentFlag");
                    } else if (rowData.orderStatus == "待到场") {
                        //插入样式，注意：当一个页面中包含多个jqgrid的时候，addClass的时候不能只根据id来添加，需要在id之前加上tableId '#report_grid_table '
                        $('#dataGrid ' + '#' + ids[i]).find("td").addClass("differentFlag1");
                    }
                }
            }
        }
    });

    function downloadData() {
        // ajaxLoading('container1');
        // setTimeout(function () {
        //     $(".datagrid-mask").remove();
        //     $(".datagrid-mask-msg").remove();
        // },2000);
        var serial = $('#serial').val();
        var orderStatus = $('#orderStatus').val();
        var orderName = $('#orderName').val();
        var createDate1 = $('#createDate1').val();
        var createDate2 = $('#createDate2').val();
        var orderTypeName = $('#orderTypeName').val();
        var createUser = $('#createUser').val();
        window.location.href = encodeURI(encodeURI('<c:url value="/web/admin/taskOrder/exportOrder.action" />?serial=' + serial + '&orderStatus='
            + orderStatus + '&orderName=' + orderName + '&createDate1=' + createDate1 + '&createDate2=' + createDate2 +  '&orderTypeName='
            + orderTypeName + '&createUser=' + createUser));
    }

    function detail(id) {
        vLayuiUtil.openLayerWindow('wTaskOrderDetail', '<c:url value="/web/admin/taskOrder/detail.action?id="/>' + id, '详情', ['80%', '95%'])
    }

    function editTaskOrder(id) {
        vLayuiUtil.openLayerWindow('wTaskOrder', '<c:url value="/web/admin/taskOrder/editPage.action?id="/>' + id, '编辑', ['80%', '95%'])
    }

    function log(id) {
        vLayuiUtil.openLayerWindow('wTaskOrderLog', '<c:url value="/web/admin/taskOrder/logPage.action?id="/>' + id, '日志', ['50%', '50%'])
    }

    function submitRemove(id) {
        js.confirm('你确认要删除这条数据吗？', function () {
            $.ajax({
                type: 'post',
                url: "<c:url value="/web/admin/taskOrder/delete.action" />",
                data: {"id": id},
                dataType: 'json',
                async: false,
                success: function (data) {
                    js.showMessage(data.msg);
                    $('#dataGrid').dataGrid('refresh');
                },
                error: function () {
                    js.showMessage("出错了，请联系管理员");
                }
            });
        })
    }

    //指定岗位和人员 type:0人员，1：岗位
    function assign(type) {
        var ids = $("#dataGrid").jqGrid("getGridParam", "selarrrow");
        if (ids.length == 0) {
            js.showMessage("请选择要指派的任务单");
            return false;
        }
        if (ids.length > 1) {
            js.showMessage("只能选择一条任务单进行指派");
            return false;
        }
        var id = ids[0];
        var ret = $("#dataGrid").jqGrid('getRowData', id);
        console.log("11="+ ret.orderStatusName);
        if (ret.orderStatusName != '待接单'){
            js.showMessage("只能指派待接单工单");
            return false;
        }
        if (type == 0) {
            vLayuiUtil.openLayerIframMaxWindow('wAssignUser', '<c:url value="/web/admin/taskOrder/assignUser.action?taskOrderId="/>' + ids, '指定接单人')
        }
        if (type == 1) {
            vLayuiUtil.openLayerIframMaxWindow('wAssignPosition', '<c:url value="/web/admin/taskOrder/assignPosition.action?taskOrderId="/>' + ids, '指定岗位')
        }
    }


    $("#createDate1").datetimepicker({
        format: 'yyyy-mm-dd',
        language: 'zh-CN',
        minView: "month",//设置只显示到月份
        autoclose: true,//选中关闭
        todayBtn: true//今日按钮
    });
    $("#createDate2").datetimepicker({
        format: 'yyyy-mm-dd',
        language: 'zh-CN',
        minView: "month",//设置只显示到月份
        autoclose: true,//选中关闭
        todayBtn: true//今日按钮
    });

    // 时间选择器互动（结束时间不得早于开始时间）
    $("#createDate1").on('changeDate', function (ev) {
        $("#createDate2").datetimepicker('setStartDate', ev.date);
        var d = $("#createDate2").val();
        if (d) {
            var date = new Date(d.replace(/-/g, '/'));
            if (date != 'Invalid Date' && date < ev.date) {
                $("#createDate2").datetimepicker('setDate', ev.date)
            }
        }
    });


</script>
